package org.bistu.course.controller;

import java.util.List;

import org.bistu.course.bean.Student;
import org.bistu.course.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class StudentController {
	@Autowired
	private StudentService studentService;
	
	@RequestMapping("/")
	public String first(Model model) {
		List<Student> students = studentService.findAll();
		model.addAttribute("students",students);
		return "listStudent";
	}
	
	@RequestMapping("/listStudent")
	public String listStudent(Model model) {
		List<Student> students = studentService.findAll();
		model.addAttribute("students",students);
		return "listStudent";
	}
	
	@RequestMapping("/toAdd")
	public String toAdd() {
		return "/addStudent";
	}
	
	@RequestMapping("/add")
	public String addStudent(Student student) {
		studentService.addStudent(student);
		return "redirect:/listStudent";
	}
	
	@RequestMapping("/toUpdata")
	public String toUpdata(Model model,int id) {
		Student student = studentService.findById(id);
		model.addAttribute("student", student);
		return "/updataStudent";
	}
	
	@RequestMapping("/updata")
	public String updataStudent(Student student) {
		studentService.updataStudent(student);
		return "redirect:/listStudent";
	}
	
	@RequestMapping("/delete")
	public String deleteStudent(int id) {
		studentService.deleteStudent(id);
		return "redirect:/listStudent";
	}
	
}
